package com.seaside.park.api.school;

import java.util.Date;

import com.seaside.parkCommon.service.AlarmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seaside.common.bean.Result;
import com.seaside.parkCommon.bean.Alarm;
import com.seaside.parkCommon.bean.ExceptionAlarm;
import com.seaside.parkCommon.bean.User;
import com.seaside.parkCommon.mapper.AlarmOperateMapper;
import com.seaside.parkCommon.mapper.ExceptionAlarmMapper;
import com.seaside.parkCommon.mapper.UserMapper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags="告警展示")
@RestController
@RequestMapping("/api/school/alarm")
public class SchoolAlarmAPI {

    @Autowired
    private AlarmService alarmService;
    
    @Autowired
    private ExceptionAlarmMapper eaMapper;
    
    @Autowired
    private UserMapper userMapper;
    
    @Autowired
    private AlarmOperateMapper alarmOperateMapper;

    @ApiOperation(value = "告警列表", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header",name="bearer",dataTypeClass=String.class,required=true,value="用户token"),
            @ApiImplicitParam(paramType="query",name="curpage",dataTypeClass=Integer.class,required=true,value="当前页")
    })
    @PostMapping("/list")
    public Result<IPage<Alarm>> list(@ApiIgnore Authentication auth, int curpage) {
    	
        Page<ExceptionAlarm> page = new Page<>(curpage, 15, false);
        
        User user = userMapper.selectById(auth.getName());
        if (user == null) {
        	return Result.Error("未找到该用户！");
        }
        
        QueryWrapper<ExceptionAlarm> queryWrapper = new QueryWrapper<>();
        if ("school".equals(user.getUserType())) {
        	queryWrapper.eq("village_id", user.getVillageId());
        } else {
        	return Result.Error("无权查看！");
        }
        
        queryWrapper.orderByDesc("alarm_time");
        
        Result<IPage<ExceptionAlarm>> iPageRet = alarmService.getExceptionAlarmListPage(page, queryWrapper);
        if (iPageRet.isSucess()) {
        	IPage<ExceptionAlarm> alarmPage = iPageRet.getData();
        	return Result.OK("", alarmPage.getRecords());
        }
        return Result.Error(iPageRet.getMsg());
    }
    
    @ApiOperation(value = "告警分页列表", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header",name="bearer",dataTypeClass=String.class,required=true,value="用户token"),
            @ApiImplicitParam(paramType="query",name="curpage",dataTypeClass=Integer.class,required=true,value="当前页")
    })
    @PostMapping("/page")
    public Result<IPage<ExceptionAlarm>> getAlarmListPage(@ApiIgnore Authentication auth, int curpage) {
    	
        Page<ExceptionAlarm> page = new Page<>(curpage, 15, false);
        
        User user = userMapper.selectById(auth.getName());
        if (user == null) {
        	return Result.Error("未找到该用户！");
        }
        
        QueryWrapper<ExceptionAlarm> queryWrapper = new QueryWrapper<>();
        if ("school".equals(user.getUserType())) {					// 物业管理可查看停车场所有告警，并可处理
        	queryWrapper.eq("village_id", user.getVillageId());
        } else {
        	return Result.Error("无权查看！");
        }
        
        queryWrapper.orderByDesc("alarm_time");
        
        Result<IPage<ExceptionAlarm>> iPageRet = alarmService.getExceptionAlarmListPage(page, queryWrapper);
        
        return iPageRet;
    }
    
    @ApiOperation(value = "告警处理", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header",name="bearer",dataTypeClass=String.class,required=true,value="用户token"),
            @ApiImplicitParam(paramType="query",name="alarmId",dataTypeClass=Integer.class,required=true,value="告警ID")
    })
    @PostMapping("/handle")
    public Result<Boolean> doAlarmHandle(@ApiIgnore Authentication auth, 
    		@RequestParam Integer alarmId) {
    	
    	if (alarmId == null) {
    		return Result.Error("参数错误！");
    	}

        User user = userMapper.selectById(auth.getName());
        if (user == null) {
        	return Result.Error("用户错误！");
        }
        
        ExceptionAlarm eAlarm = eaMapper.selectById(alarmId);
        if (eAlarm != null) {
        	eAlarm.setStatus("1");
        	eAlarm.setHandler(user.getUserGuid());
        	eAlarm.setHandlerName(user.getUserName());
        	eAlarm.setHandleTime(new Date());
        	eaMapper.updateById(eAlarm);
        }
        
    	return Result.OK("已处理");
    }
    
//    @ApiOperation(value = "查询告警处理过程", response = Result.class)
//    @ApiImplicitParams({
//            @ApiImplicitParam(paramType="header",name="bearer",dataTypeClass=String.class,required=true,value="用户token"),
//            @ApiImplicitParam(paramType="query",name="alarmId",dataTypeClass=Integer.class,required=true,value="告警ID")
//    })
//    @PostMapping("/operate/list")
//    public Result<List<AlarmOperate>> getAlarmOperateList(@ApiIgnore Authentication auth, 
//    		@RequestParam Integer alarmId) {
//    	
//    	if (alarmId == null) {
//    		return Result.Error("参数错误！");
//    	}
//    	
//    	QueryWrapper<AlarmOperate> queryWrapper = new QueryWrapper<>();
//    	queryWrapper.eq("alarm_id", alarmId);
//    	queryWrapper.orderByDesc("handle_time");
//    	List<AlarmOperate> operateList = alarmOperateMapper.selectList(queryWrapper);
//    	
//    	return Result.OK("查询成功！", operateList);
//    }
}